package com.example.composelauncher

import android.os.Bundle
import android.view.View
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.Crossfade
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntRect
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.composelauncher.ui.theme.ComposeLauncherTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class LauncherActivity : ComponentActivity() {

    private var inset: IntRect by mutableStateOf(IntRect.Zero)

    @ExperimentalAnimationApi
    @ExperimentalFoundationApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        window.decorView.apply {
            systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)

            setOnApplyWindowInsetsListener { _, insets ->
                with(insets!!) {
                    inset = IntRect(
                        IntOffset(systemWindowInsetLeft, systemWindowInsetTop),
                        IntOffset(systemWindowInsetRight, systemWindowInsetBottom)
                    )
                    insets.consumeSystemWindowInsets()
                }
            }
        }

        setContent {
            ComposeLauncherTheme(darkTheme = false) {
                LauncherWorkspaceHoisted(inset)
            }
        }
    }

    override fun onBackPressed() {
        // ignore
    }
}

@ExperimentalAnimationApi
@ExperimentalFoundationApi
@Composable
fun LauncherWorkspaceHoisted(inset: IntRect = IntRect.Zero) {
    val launcherViewModel = viewModel(modelClass = LauncherViewModel::class.java)
    val appList by launcherViewModel.appIconList.observeAsState(mutableListOf())
    val appLoading by launcherViewModel.allAppLoading.observeAsState(true)

    Crossfade(targetState = appLoading) {
        if (it) {
            Box(modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight()) {
                CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
            }
        } else {
            LauncherWorkspace(
                modifier = Modifier
                    .fillMaxWidth()
                    .fillMaxHeight(),
                iconList = appList,
                dragPadding = inset,
                onItemClick = { app ->
                    launcherViewModel.onAppIconClicked(app)
                },
                reorderResult = { list ->
                    launcherViewModel.onAppReorder(list)
                }
            )
        }
    }
}